import java.io.File;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLStreamException;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileSystemUtils;


public class ReadDb2XML {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
		
		BasicDao dao = (BasicDao) factory.getBean("dao");
		List<Map<String, Object>> data = dao.queryForList("SELECT  * FROM MSTDIV");
		try {
            Object2XML.listToXml(data,  new OutputStreamWriter(
                    new FileOutputStream("D:\\Testxml.xml"),
                    Charset.forName("UTF-8").newEncoder()));
            		//	new FileWriter("D:\\Testxml.xml"));
            
            //UTF-8
            Object2CSV.listToCsv(data,
            		             new OutputStreamWriter(
                                          new FileOutputStream("D:\\Testutf8.txt"),
                                          Charset.forName("UTF-8").newEncoder()),
                                 '|');
            
            //US-ASCII (ANSI)
            Object2CSV.listToCsv(data, 
            		new FileWriter("D:\\Testascii.txt"),
                    '|');
            
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        catch(XMLStreamException e) {
            e.printStackTrace();
        }

	}

}
